package com.streambolics.std;

/**
 * A TextBuilder that generates a simple ANSI plain text.
 * 
 * @author Sauron
 * 
 */

public class SimpleTextBuilder implements TextBuilder
{

    private StringBuilder       _StringBuilder;
    private static final String ENDPARA = "\r\n\r\n";

    /**
     * Creates a standalone SimpleTextBuilder.
     * 
     * The created text will be available through the toString() function.
     */

    public SimpleTextBuilder()
    {
        init (new StringBuilder ());
    }

    /**
     * Creates a SimpleTextBuilder that will append the text to an existing
     * String Builder.
     * 
     * Note that a call to toString() will return the complete contents of the
     * StringBuilder, not just the contents added by this TextBuilder.
     * 
     * @param aStringBuilder
     *            The StringBuilder to append text to.
     */

    public SimpleTextBuilder(StringBuilder aStringBuilder)
    {
        init (aStringBuilder);
    }

    @Override
    public void addAnchor (String aURL, String aText)
    {
        addText (aText, ": ", aURL);
    }

    @Override
    public void addParagraph (String... aTexts)
    {
        openParagraph ();
        addText (aTexts);
        closeParagraph ();
    }

    @Override
    public void addText (String... aTexts)
    {
        for (String s : aTexts)
        {
            _StringBuilder.append (s);
        }
    }

    @Override
    public void closeParagraph ()
    {
        addText (ENDPARA);
    }

    /**
     * The StringBuilder used to generate content.
     * 
     * @return The StringBuilder being used.
     */

    public StringBuilder getStringBuilder ()
    {
        return _StringBuilder;
    }

    private void init (StringBuilder aStringBuilder)
    {
        _StringBuilder = aStringBuilder;
    }

    @Override
    public void openParagraph ()
    {
    }

    @Override
    public String toString ()
    {
        return _StringBuilder.toString ();
    }
}
